function drawCode(canvas, image) {
  var str = ''
  var nums = getAllLetter()
  // 获取HTML端画布
  // var canvas = this.$refs.verifyCanvas
  // console.log(canvas)
  // 获取画布2D上下文
  var context = canvas.getContext('2d')
  // console.log(context)
  // context.clearRect(0, 0, canvas.width, canvas.height)
  // 画布填充色
  context.fillStyle = 'darkorange'
  // 清空画布
  context.fillRect(0, 0, canvas.width, canvas.height)
  // 画3条随机线
  for (let i = 0; i < 3; i++) {
    drawline(canvas, context)
  }
  // 画30个随机点
  for (let i = 0; i < 30; i++) {
    drawDot(canvas, context)
  }
  // 设置字体颜色
  context.fillStyle = 'white'
  // 设置字体
  context.font = '100px Arial'
  let rand = []
  let x = []
  let y = []
  for (var i = 0; i < 4; i++) {
    rand.push(rand[i])
    rand[i] = nums[Math.floor(Math.random() * nums.length)]
    x[i] = i * 70 + 10
    y[i] = Math.random() * 20 + 100
    context.fillText(rand[i], x[i], y[i])
  }
  str = rand.join('').toUpperCase()
  console.log('验证码', str)
  convertCanvasToImage(canvas, image)
  return str
}
function getAllLetter() {
  // 获取数组
  var letterStr = '1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'
  return letterStr.split(',')
}
function drawline (canvas, context) {
  // 画随机线函数
  // 随机线的起点x坐标是画布x坐标0位置，y坐标是画布高度的随机数
  context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height))
  // 随机线的终点x坐标是画布宽度，y坐标是画布高度的随机数
  context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height))
  // 随机线宽
  context.lineWidth = 0.5
  // 随机线描边属性
  context.strokeStyle = 'rgba(50,50,50,0.3)'
  // 描边，即起点描到终点/
  context.stroke()
}
// 随机点(所谓画点其实就是画1px像素的线，方法不再赘述)
function drawDot (canvas, context) {
  var px = Math.floor(Math.random() * canvas.width)
  var py = Math.floor(Math.random() * canvas.height)
  context.moveTo(px, py)
  context.lineTo(px + 1, py + 1)
  context.lineWidth = 0.2
  context.stroke()
}
// 绘制图片
function convertCanvasToImage (canvas, image) {
  // document.getElementById('verifyCanvas').style.display = 'none'
  canvas.style.display = 'none'
  // var image = this.$refs.code_img
  image.src = canvas.toDataURL('image/png')
  return image
}
export default drawCode
